package com.rzjm.exercise.module.controller;

import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.Groups;
import com.rzjm.exercise.module.service.GroupsService;
import com.rzjm.exercise.sys.system.user.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
/**
 * 集团
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2020-12-15 22:36:23
 */
@Controller
@RequestMapping("/module/group")
public class GroupsController {
    @Resource
    private GroupsService groupsService;

    @Log("查询集团页面")
    @GetMapping()
    @RequiresPermissions("module:group:group")
    String Shop() {
        return "module/group/group";
    }

    @Log("查询集团")
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("module:group:group")
    public DataTableLayUI<Groups> list(DataTableLayUI<Groups> condition) {
        DataTableLayUI<Groups> dt = groupsService.pageSearchLayUI(condition);
        return dt;
    }

    @Log("新增集团页面")
    @GetMapping("/add")
    @RequiresPermissions("module:group:add")
    String add(Model model) {
        Groups group = new Groups();
        model.addAttribute("group", group);
        return "module/group/detail";
    }


    @Log("编辑集团页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:group:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        Groups group = groupsService.findById(id);
        model.addAttribute("group", group);
        return "module/group/detail";
    }

    /**
     * 保存
     */
    @Log("新增集团")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:group:add")
    public R save(@Valid Groups group) {
        if (groupsService.save(group)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("更新集团")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:group:edit")
    public R update(@Valid Groups group) {
        groupsService.updateById(group);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除集团")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:group:remove")
    public R remove( Integer id) {
        if (groupsService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }
}
